package com.shijunhao.graduation.service.business.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.dto.BirthdayCulDto;
import com.shijunhao.graduation.domain.entity.*;
import com.shijunhao.graduation.domain.vo.PieCulVo;
import com.shijunhao.graduation.domain.vo.UserCulVo;
import com.shijunhao.graduation.enums.DictCode;
import com.shijunhao.graduation.mapper.BusinessHotInfoMapper;
import com.shijunhao.graduation.service.business.*;
import com.shijunhao.graduation.service.scheduled.HotInfoScheduledService;
import com.shijunhao.graduation.service.system.IOrgUserService;
import com.shijunhao.graduation.service.system.ISystemDictService;
import com.shijunhao.graduation.service.system.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-10
 */
@Service
public class BusinessHotInfoServiceImpl extends BaseServiceImpl<BusinessHotInfoMapper, BusinessHotInfo> implements IBusinessHotInfoService {

    @Resource
    private ISystemDictService systemDictService;
    @Resource
    private HotInfoScheduledService hotInfoScheduledService;

    @Resource
    private IBusinessNoteService noteService;
    @Resource
    private IBusinessNoteDiscussService businessNoteDiscussService;
    @Resource
    private IOrgUserService orgUserService;
    @Resource
    private IBusinessForumService businessForumService;
    @Resource
    private IBusinessForumDiscussService businessForumDiscussService;


    @Override
    public List<PieCulVo> noteGenderInfo() {
        List<PieCulVo> pieCulVos = this.getBaseMapper().noteGenderInfo(1);
        for (PieCulVo vo : pieCulVos) {
            if ("1".equals(vo.getName())) {
                vo.setName("男");
            } else if ("0".equals(vo.getName())) {
                vo.setName("女");
            } else {
                vo.setName("未知");
            }
        }
        return pieCulVos;
    }

    @Override
    public List<PieCulVo> noteAgeInfo() {
        List<PieCulVo> result = new ArrayList<>();
        result.add(new PieCulVo("未知", 0));
        result.add(new PieCulVo("0-15", 0));
        result.add(new PieCulVo("15-25", 0));
        result.add(new PieCulVo("25-35", 0));
        result.add(new PieCulVo("35-45", 0));
        result.add(new PieCulVo("45+", 0));
        List<BirthdayCulDto> dtos = this.getBaseMapper().noteAgeInfo(1);
        Date date = new Date();
        for (BirthdayCulDto dto : dtos) {
            if (dto.getDate() == null)
                result.get(0).addValue(1);
            else {
                int age = date.getYear() - dto.getDate().getYear();
                int ca = (age < 15 ? 1 : 0) + (age < 25 ? 1 : 0) + (age < 35 ? 1 : 0) + (age < 45 ? 1 : 0);
                switch (ca) {
                    //45+
                    case 0:
                        result.get(5).addValue(1);
                        break;
                    //35-45
                    case 1:
                        result.get(4).addValue(1);
                        break;
                    //25-35
                    case 2:
                        result.get(3).addValue(1);
                        break;
                    //15-25
                    case 3:
                        result.get(2).addValue(1);
                        break;
                    //0-15
                    case 4:
                        result.get(1).addValue(1);
                        break;
                }
            }
        }
        return result;
    }

    @Override
    public BusinessHotInfo getHotInfo(String tableName, Long nid) {
        SystemDictItem table = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), tableName);
        return hotInfoScheduledService.getHotInfo(table.getDictValue(), nid);
    }

    @Override
    public boolean insertDefaultHotInfo(String tableName, Long nid) {
        BusinessHotInfo hotInfo = new BusinessHotInfo();
        hotInfo.setBads(0);
        hotInfo.setGoods(0);
        hotInfo.setLooks(0);
        hotInfo.setNid(nid);
        hotInfo.setTop(false);
        SystemDictItem table = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), tableName);
        hotInfo.setTableName(table.getDictValue());
        return this.insertOne(hotInfo);
    }

    @Override
    public BusinessHotInfo getById(Long id) {
        return hotInfoScheduledService.getHotInfo(id);
    }

    @Override
    public UserCulVo toUserCulVo(Long uid) {
        UserCulVo userCulVo = new UserCulVo();
        int noteCount = noteService.selectCount(Wrappers.lambdaQuery(BusinessNote.class).eq(BaseEntity::getCreateBy, uid).eq(BusinessNote::getType, 1));
        userCulVo.setNoteCount(noteCount);
        int postCount = noteService.selectCount(Wrappers.lambdaQuery(BusinessNote.class).eq(BaseEntity::getCreateBy, uid).eq(BusinessNote::getType, 0));
        userCulVo.setPostCount(postCount);
        int forumCount = businessForumService.selectCount(Wrappers.lambdaQuery(BusinessForum.class).eq(BaseEntity::getCreateBy, uid));
        userCulVo.setForumCount(forumCount);
        int forumDiscuss = businessForumDiscussService.selectCount(Wrappers.lambdaQuery(BusinessForumDiscuss.class).eq(BaseEntity::getCreateBy, uid));
        int discussCount = businessNoteDiscussService.selectCount(Wrappers.lambdaQuery(BusinessNoteDiscuss.class).eq(BaseEntity::getCreateBy, uid));
        userCulVo.setDiscussCount(forumDiscuss+discussCount);

        int sum = selectByCondition(Wrappers.lambdaQuery(BusinessHotInfo.class)
                .eq(BaseEntity::getCreateBy, uid))
                .stream()
                .mapToInt(BusinessHotInfo::getGoods)
                .sum();
        userCulVo.setGoodsCount(sum);
        //设置用户注册时间
        Date date = orgUserService.getById(uid).getCreateDate();
        userCulVo.setUseYear(culDate(date));
        return userCulVo;
    }

    @Override
    public UserCulVo toAdminCulVo(Long uid) {
        UserCulVo userCulVo = new UserCulVo();
        int noteCount = noteService.selectCount(Wrappers.lambdaQuery(BusinessNote.class).eq(BusinessNote::getType, 1));
        userCulVo.setNoteCount(noteCount);
        int postCount = noteService.selectCount(Wrappers.lambdaQuery(BusinessNote.class).eq(BusinessNote::getType, 0));
        userCulVo.setPostCount(postCount);
        int forumCount = businessForumService.selectCount(Wrappers.lambdaQuery(BusinessForum.class));
        userCulVo.setForumCount(forumCount);
        userCulVo.setDiscussCount(0);
        userCulVo.setGoodsCount(0);
        Date date = orgUserService.getById(uid).getCreateDate();
        userCulVo.setUseYear(culDate(date));
        return userCulVo;
    }

    private String culDate(Date date){
        Date time = new Date(new Date().getTime() - date.getTime());
        StringBuilder useYear = new StringBuilder();
        if (time.getYear() > 1970) {
            useYear.append(time.getYear() - 1970).append("年");
        }
        if (time.getMonth() > 0) {
            useYear.append(time.getMonth()).append("月");
        }
        if (time.getDay() > 0) {
            useYear.append(time.getDay()).append("天");
        }
        return useYear.toString();
    }
}
